package com.example.diemtinvietnam;

import java.util.ArrayList;
import java.util.HashMap;
import com.example.diemtinvietnam.LazyAdapter;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.view.View;
import android.app.AlertDialog;
import android.app.ProgressDialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.Gravity;

public class MainActivity extends Activity {

	private jsonParse jsonP;
	static final String KEY_ID = "id";
	static final String KEY_TITLE = "title";
	static final String KEY_SOURCE = "maindomain";
	static final String KEY_DATE = "date";
	static final String KEY_LINK = "link";
	static final String KEY_IMAGE = "image";
	ListView list;
    LazyAdapter adapter;
    public ArrayList<HashMap<String, String>> itemNewsList;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		showNewsFromUrl("http://diemtinvietnam.vn/Home/getHotNewsFromFile?datetime=null&orderby=0");		
	    
	}	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	public void showNewsFromUrl(String url){	

		Log.e("onCreate", "ok1");
		jsonP=new jsonParse(url);
		Log.e("onCreate", "ok2");
		final ProgressDialog dialog = ProgressDialog.show(this, "Diemtinvietnam.vn","Loading...", true);
		new Thread(new Runnable(){
				public void run(){
					try {
							//---simulate doing something lengthy---
							Thread.sleep(5000);
							//while(jsonP.parsingComplete);
							//---dismiss the dialog---
							dialog.dismiss();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
				}
		}).start();	
		
		jsonP.fetchJSON();			
	    while(jsonP.parsingComplete);
	    Log.e("doc title showNewsFromUrl", jsonP.getTitle());
	    itemNewsList = new ArrayList<HashMap<String, String>>();
	    itemNewsList=jsonP.getItemNewsList();
	    Log.e("itemNewsList", "ok1");	
	    list=(ListView)findViewById(R.id.list);
		////////// Viec customize listview duoc thuc hien o LazayAdapter
        adapter=new LazyAdapter(this, itemNewsList);        
        list.setAdapter(adapter);
        Log.e("list.setAdapter(adapter);", "ok2");

        list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
					String link=String.valueOf(itemNewsList.get(position).get("link"));
					Log.e("user click","pos="+String.valueOf(position)+",id="+String.valueOf(id)+" "+link);
					final ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "Diemtinvietnam.vn","Loading...", true);
					new Thread(new Runnable(){
							public void run(){
								try {
										//---simulate doing something lengthy---
										Thread.sleep(5000);
										//while(jsonP.parsingComplete);
										//---dismiss the dialog---
										dialog.dismiss();
									} catch (InterruptedException e) {
										e.printStackTrace();
									}
							}
					}).start();	
					Log.e("Log","ket thuc loading, bat dau goi View Web");
					Intent vi = new Intent(MainActivity.this, viewWeb.class);
					vi.putExtra("link", link);					
					startActivity(vi);
					Log.e("Log","ket thuc  goi View Web");
						
			}
		});	
	}

}
